package cn.elwy.rcp.ui.explorer.create.wizard;

import java.io.File;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cn.elwy.common.util.AssertUtil;
import cn.elwy.common.util.PathUtil;
import cn.elwy.common.util.io.FileUtil;
import cn.elwy.osgi.config.util.ResourceUtil;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;
import cn.elwy.rcp.ui.explorer.project.ProjectUtil;
import cn.elwy.rcp.ui.util.PathSelectedUtil;
import cn.elwy.rcp.ui.wizard.BaseWizardPage;

public class ProjectInfoWizardPage extends BaseWizardPage {

	public static final String ID = "ProjectSvnWizardPage";

	private Button projectPathButton;
	private Text projectPathText;
	private Label projectPathLabel;
	private Text projectNameText;
	private Label projectVersionLabel;
	private Text projectVersionText;
	private Composite composite;
	private Label resourcePathLabel;
	private Label outputPathLabel;
	private Text checkoutPathText;
	private Text outputPathText;
	private Button resourcePathButton;
	private Button outputPathButton;
	private Composite composite_2;
	private Button useDefaultPathButton;

	private boolean pageState;

	private ProjectConfig projectConfig;

	/**
	 * Create the wizard.
	 */
	public ProjectInfoWizardPage() {
		super(ID);
		setTitle("新建构建工程");
		setDescription("设置构建工程的基本配置项");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = createUI(parent);
		setControl(container);

		addListener();
		init();
	}

	private Composite createUI(Composite parent) {
		ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		Composite container = new Composite(scrolledComposite, SWT.NULL);

		container.setLayout(new GridLayout(4, false));

		Label projectNameLabel = new Label(container, SWT.NONE);
		projectNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		projectNameLabel.setText("工程名称(&P)：");

		composite = new Composite(container, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);

		projectNameText = new Text(composite, SWT.BORDER);
		projectNameText.setFocus();
		projectNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		projectVersionLabel = new Label(composite, SWT.NONE);
		projectVersionLabel.setText("版本号(&V)：");

		projectVersionText = new Text(composite, SWT.BORDER);
		GridData gd_projectVersionText = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_projectVersionText.widthHint = 60;
		projectVersionText.setLayoutData(gd_projectVersionText);

		composite_2 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 4, 1));

		useDefaultPathButton = new Button(composite_2, SWT.CHECK);

		useDefaultPathButton.setSelection(true);
		useDefaultPathButton.setText("使用默认位置");

		projectPathLabel = new Label(container, SWT.NONE);
		projectPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		projectPathLabel.setText("工程路径：");

		projectPathText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		projectPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		projectPathButton = new Button(container, SWT.NONE);
		projectPathButton.setEnabled(false);
		projectPathButton.setText("浏览");

		resourcePathLabel = new Label(container, SWT.NONE);
		resourcePathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		resourcePathLabel.setText("SVN检出目录：");

		checkoutPathText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		checkoutPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		checkoutPathText.setEditable(true);

		resourcePathButton = new Button(container, SWT.NONE);
		resourcePathButton.setText("浏览");

		outputPathLabel = new Label(container, SWT.NONE);
		outputPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		outputPathLabel.setText("打包输出路径：");

		outputPathText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		outputPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		outputPathText.setEditable(true);

		outputPathButton = new Button(container, SWT.NONE);
		outputPathButton.setText("浏览");

		container.pack();
		scrolledComposite.setContent(container);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		Point size = container.getSize();
		scrolledComposite.setMinSize(size);

		return scrolledComposite;
	}

	private void addListener() {
		projectNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				boolean selection = useDefaultPathButton.getSelection();
				if (selection) {
					String projectName = projectNameText.getText().trim();
					String version = projectVersionText.getText().trim();
					File file = new File(ResourceUtil.getWorkspacePath().getAbsolutePath(), projectName + "_" + version);
					projectPathText.setText(PathUtil.getFormatPath(file));
				}
				validatePage();
			}
		});
		projectVersionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				boolean selection = useDefaultPathButton.getSelection();
				if (selection) {
					String projectName = projectNameText.getText().trim();
					String version = projectVersionText.getText().trim();
					File file = new File(ResourceUtil.getWorkspacePath().getAbsolutePath(), projectName + "_" + version);
					projectPathText.setText(PathUtil.getFormatPath(file));
				}
				validatePage();
			}
		});
		useDefaultPathButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				boolean selection = useDefaultPathButton.getSelection();
				if (selection) {
					projectPathText.setEditable(false);
					projectPathButton.setEnabled(false);
					String projectName = projectNameText.getText().trim();
					String version = projectVersionText.getText().trim();
					File file = new File(ResourceUtil.getWorkspacePath().getAbsolutePath(), projectName + "_" + version);
					projectPathText.setText(PathUtil.getFormatPath(file));
				} else {
					projectPathText.setEditable(true);
					projectPathButton.setEnabled(true);
				}
				validatePage();
			}
		});
		projectPathText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		checkoutPathText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		outputPathText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});

		resourcePathButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String workspacePath = ResourceUtil.getWorkspacePath().getAbsolutePath();
				String text = checkoutPathText.getText();
				if (AssertUtil.isNotEmpty(text)) {
					workspacePath = text;
				}
				String selectDirectory = PathSelectedUtil.getSelectDirectory(getShell(), "选择目录", "选择目录", workspacePath);
				if (selectDirectory != null) {
					checkoutPathText.setText(PathUtil.getFormatPath(selectDirectory));
				}
			}
		});
		projectPathButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String workspacePath = ResourceUtil.getWorkspacePath().getAbsolutePath();
				String text = projectPathText.getText();
				if (AssertUtil.isNotEmpty(text)) {
					workspacePath = text;
				}
				String selectDirectory = PathSelectedUtil.getSelectDirectory(getShell(), "选择目录", "选择目录", workspacePath);
				if (selectDirectory != null) {
					projectPathText.setText(PathUtil.getFormatPath(selectDirectory));
				}
			}
		});
		outputPathButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String workspacePath = ResourceUtil.getWorkspacePath().getAbsolutePath();
				String text = outputPathText.getText();
				if (AssertUtil.isNotEmpty(text)) {
					workspacePath = text;
				}
				String selectDirectory = PathSelectedUtil.getSelectDirectory(getShell(), "选择目录", "选择目录", workspacePath);
				if (selectDirectory != null) {
					outputPathText.setText(PathUtil.getFormatPath(selectDirectory));
				}
			}
		});
	}

	private void init() {
		projectConfig = ((NewBuilderProjectWizard) getWizard()).getProjectConfig();
		projectNameText.setText(projectConfig.getProjectName());
		projectVersionText.setText(projectConfig.getVersion());
		String path = projectConfig.getProjectPath();
		if (AssertUtil.isEmpty(path)) {
			path = ResourceUtil.getWorkspacePath().getAbsolutePath();
			path = path.replace("\\", "/");
		}
		projectPathText.setText(path);
		checkoutPathText.setText(projectConfig.getResourcePath());
		outputPathText.setText(projectConfig.getOutputPath());
	}

	public boolean validate() {
		pageState = validateProject() && validateProjectPath() && validateCheckoutPath() && validateOutputPath();
		if (pageState) {
			setErrorMessage(null);
		}
		setPageComplete(pageState);
		return pageState;
	}

	private boolean validateProject() {
		String projectName = projectNameText.getText();
		if (AssertUtil.isEmpty(projectName)) {
			setErrorMessage("请输入工程名称！");
			return false;
		}
		String version = projectVersionText.getText().trim();
		if (AssertUtil.isEmpty(version)) {
			setErrorMessage("请输入版本号！");
			return false;
		}
		boolean existProject = ProjectUtil.isExistProject(projectName + "_" + version);
		if (existProject) {
			setErrorMessage("工程已经存在！");
			return false;
		}
		String projectPath = projectPathText.getText().trim();
		if (AssertUtil.isEmpty(projectPath)) {
			setErrorMessage("工程路径不能为空！");
			return false;
		}
		// if (false) {
		// setErrorMessage("本地路径不存在！");
		// return false;
		// }
		return true;
	}

	private boolean validateProjectPath() {
		String path = projectPathText.getText().trim();
		if (AssertUtil.isEmpty(path)) {
			setErrorMessage("工程路径不能为空！");
			return false;
		}
		IPath fromOSString = Path.fromOSString(path);
		boolean canCreateDirectory = FileUtil.canCreateDirectory(fromOSString.toFile());
		if (!canCreateDirectory) {
			setErrorMessage("工程路径不合法！");
			return false;
		}
		return true;
	}

	private boolean validateCheckoutPath() {
		if (AssertUtil.isEmpty(checkoutPathText.getText())) {
			setErrorMessage("SVN检出目录不能为空！");
			return false;
		}
		String path = checkoutPathText.getText().trim();
		IPath fromOSString = Path.fromOSString(path);
		boolean canCreateDirectory = FileUtil.canCreateDirectory(fromOSString.toFile());
		if (!canCreateDirectory) {
			setErrorMessage("SVN检出目录不合法！");
			return false;
		}
		return true;
	}

	private boolean validateOutputPath() {
		String path = outputPathText.getText().trim();
		if (AssertUtil.isEmpty(path)) {
			setErrorMessage("打包输出路径不能为空！");
			return false;
		}
		IPath fromOSString = Path.fromOSString(path);
		boolean canCreateDirectory = FileUtil.canCreateDirectory(fromOSString.toFile());
		if (!canCreateDirectory) {
			setErrorMessage("打包输出路径不合法！");
			return false;
		}
		return true;
	}

	public Object getPageData() {
		projectConfig.setProjectName(projectNameText.getText().trim());
		projectConfig.setVersion(projectVersionText.getText().trim());
		projectConfig.setProjectPath(PathUtil.getFormatPath(projectPathText.getText().trim()));
		projectConfig.setResourcePath(PathUtil.getFormatPath(checkoutPathText.getText().trim()));
		projectConfig.setOutputPath(PathUtil.getFormatPath(outputPathText.getText().trim()));
		return projectConfig;
	}

}
